package com.vic.kjlink.order.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.vic.kjlink.order.model.Food;
import com.vic.kjlink.order.service.FoodService;

import pers.vic.boot.base.model.PageInfo;
import pers.vic.boot.base.model.Remind;

/**
 * @description:套餐表 控制层
 * @author Vic.xu
 * @date: 2021-08-30 09:37
 */
@Controller
@RequestMapping("/food")
public class FoodController extends BaseOrderController {

    @Resource
    private FoodService foodService;

    private static final String FORM = "food-form";
    private static final String LIST = "food-list";

    private static final String REDIRECT_LIST = "redirect:/food/";

    @GetMapping(value = {"", "/"})
    public String list(Food lookup, Model model) {
        PageInfo<Food> page = foodService.page(lookup);
        model.addAttribute("pager", page);
        return LIST;
    }

    @GetMapping("/0")
    public String add() {
        return FORM;
    }

    @GetMapping("{id}")
    public String update(@PathVariable int id, Model model) {
        Food entity = foodService.findById(id);
        model.addAttribute("entity", entity);
        return FORM;
    }

    @PostMapping(value = "{id}")
    public String save(@PathVariable int id, Food entity, RedirectAttributes attributes) {
        entity.setId(id);
        foodService.save(entity);
        attributes.addFlashAttribute(Remind.success());
        return REDIRECT_LIST;

    }

    @GetMapping(value = "delete/{id}")
    public String delete(@PathVariable int id, RedirectAttributes attributes) {
        foodService.delete(id);
        attributes.addFlashAttribute(Remind.success());
        return REDIRECT_LIST;
    }

}
